package com.nice.nice201110.service;

import com.nice.nice201110.entity.TLog;
import com.nice.nice201110.entity.TOrganization;
import com.nice.nice201110.mapper.TLogMapper;
import com.nice.nice201110.mapper.TOrganizationMapper;
import com.nice.nice201110.utils.NiceTableEnum;
import com.nice.nice201110.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.Date;
import java.util.List;

@Service
public class TorganizationService {

    @Autowired
    TOrganizationMapper tOrganizationMapper;

    @Autowired
    TLogMapper tLogMapper;

    @Autowired
    private PlatformTransactionManager transactionManager;

    private TLog tLog = new TLog();

    /** 查询数据 */
    public List<TOrganization> getList(){
        return tOrganizationMapper.selectByPrimaryKey();
    }

    /** 根据父类获取对应的组织s*/
    public List<TOrganization> getParentList(Integer parentid){
        return tOrganizationMapper.selectByParent(parentid);
    }

    /** 新增组织*/
    public Boolean insetTorganization(UserVo userVo,TOrganization tOrganization){
        try {
            int num = tOrganizationMapper.insertSelective(tOrganization);
            if(num>0){
                tLog.setDate(new Date());
                tLog.setType(0);  //操作类型（新增0，修改1，删除2）
                tLog.setDocid(tOrganization.getId());
                tLog.setUsername(userVo.getName());
                tLog.setUserid(Integer.valueOf(userVo.getId()));
                tLog.setTtable(NiceTableEnum.form_organization);
                tLog.setRemark("新增组织信息:"+tOrganization.toString());
                tLogMapper.insertSelective(tLog);
            }else{
                return false;
            }
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
        return true;
    }
    /** 删除信息 */
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public Boolean deleteTorganization(UserVo userVo, Integer id){
        try {
            int num = tOrganizationMapper.deleteByPrimaryKey(id);
            if(num>0){
                tLog.setDate(new Date());
                tLog.setType(2);  //操作类型（新增0，修改1，删除2）
                tLog.setDocid(id);
                tLog.setUsername(userVo.getName());
                tLog.setUserid(Integer.valueOf(userVo.getId()));
                tLog.setTtable(NiceTableEnum.form_organization);
                tLog.setRemark("删除组织信息:" + tOrganizationMapper.selectByPrimaryKey(id).toString());
                tLogMapper.insertSelective(tLog);
            }else{
                return false;
            }
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
        return true;
    }



}
